package someclient

import (
	"time"

	"gitee.com/firewing_group/configenv"

	mgo "gopkg.in/mgo.v2"
)

var _mongohost string
var _dbname string
var _animaldbname string
var _sessionpool []*mgo.Session

// the default db is self_mongo instead of db:kuaimao
func init() {
	_mongohost = configenv.GetEnvOrConfig("MONGOIP")
	_dbname = configenv.GetEnvOrConfig("SELF_DBNAME")
	_animaldbname = configenv.GetEnvOrConfig("KETH_DBNAME")
	_sessionpool = make([]*mgo.Session, 6, 6)
	for idx := range _sessionpool {
		session, err := mgo.Dial(_mongohost)
		if err != nil {
			panic("mongo init" + err.Error())
		}
		_sessionpool[idx] = session
	}
}

func GetDB(dbname ...string) *mgo.Database {
	idx := time.Now().Unix() % 6
	if len(dbname) != 0 {
		return _sessionpool[idx].DB(dbname[0])
	}
	return _sessionpool[idx].DB(_dbname)
}
func GetAnimalDB() *mgo.Database {
	idx := time.Now().Unix() % 6

	return _sessionpool[idx].DB(_animaldbname)
}
